<?php

namespace app\common\model\customer;

use think\Model;

class Subscribe extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'subscribe';
    protected $pk    = 'SubscribeId';

    const STATEWAIT    = 1; //进行中
    const STATEOK   = 2; //已完成
    const STATECANCEL   = 3; //已取消
    
    // 查询预约访问记录
    public function getList($where) {
        return $this->where($where)->order('SubscribeTime desc')->select();
    }

    // 添加预约回访记录
    public function addSubscribe($data){
        //客户预约回访信息
        if(!empty($data['CustomerId'])){
            $this->CustomerId = $data['CustomerId'];
        }
        //企业预约回访信息
        if(!empty($data['CompanyId'])){
            $this->CompanyId = $data['CompanyId'];
        }
        //企业摸排表回访信息
        if(!empty($data['CompanyTouchId'])){
            $this->CompanyTouchId = $data['CompanyTouchId'];
        }
        //企业摸排表回访信息
        if(!empty($data['CompanyFileId'])){
            $this->CompanyFileId = $data['CompanyFileId'];
        }

        $this->SubscribeTime = $data['SubscribeTime'];
        $this->BussinessId = $data['u_id'];
        $this->Content = $data['Content'];
        $this->Img = isset($data['Img'])?$data['Img']: '';
        $this->Status = isset($data['Status'])?$data['Status']:'1';
        $this->AddTime = date('Y-m-d H:i:s',time());
        $this->save();
        $rst = [
            'SubscribeId' => $this->SubscribeId,
        ];
        return $rst;
    }

    // 更新预约回访记录
    public function editSubscribe($data)
    {
        $subscribe = $this->where('SubscribeId', $data['SubscribeId'])->find();
        $subscribe->CustomerId = $data['CustomerId'];
        $subscribe->SubscribeTime = $data['SubscribeTime'];
        $subscribe->Content = $data['Content'];
        $subscribe->Img = $data['Img'];
        $subscribe->save();
        return $rst = [
            'SubscribeId' => $subscribe->SubscribeId,
        ];
    }
    // 更新预约回访记录状态
    public function editSubscribeStatus($data)
    {
        $subscribe = $this->where('SubscribeId', $data['SubscribeId'])->find();
        if($data['Status']==4){
            $subscribe->delete();
        }else{
            $subscribe->Status = $data['Status'];
            $subscribe->save();
        }
        return $rst = [
            'SubscribeId' => $subscribe->SubscribeId,
        ];
    }
}
